package com.weichuanbo.xwdz.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.weichuanbo.xwdz.R;
import com.weichuanbo.xwdz.app.Common;
import com.weichuanbo.xwdz.app.utils.SPUtil;
import com.weichuanbo.xwdz.app.utils.SaveBeanUtil;
import com.weichuanbo.xwdz.mvp.model.entity.UserBean;

import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


public class SplashActivity extends BaseActivity  {

    private Disposable subscribe;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_splash;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        timerDown();
    }
   private void timerDown() {
       subscribe = Flowable.timer(2000, TimeUnit.MILLISECONDS)
               .subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Consumer<Long>() {
                   @Override
                   public void accept(Long aLong) throws Exception {
                       judge();
                   }
               });
   }


    public void judge() {

        boolean isFirstLogin = (boolean) SPUtil.get(this, Common.Const.ISFIRST, false);
        Object bean = SaveBeanUtil.getBean(this, Common.Const.VIPINFO);
        if (bean != null && !TextUtils.isEmpty(bean.toString())) {
            UserBean loginBean = (UserBean) bean;
            String  ryToken = loginBean.getRongcloud_token();
        }
        if (isFirstLogin) {
//                    Intent intent = new Intent(SplashActivity.this, WelcomeActivity.class);
//                    startActivity(intent);
        } else {
            login();
        }

    }

    public void login() {
        Intent intent = new Intent(this, SelecteLoginActivity.class);
        startActivity(intent);
     /*   if (!TextUtils.isEmpty(ryToken)) {//如果token为空则没有登陆过
            //如果服务端有使用token登陆的接口,则调用token登陆，如果没有就直接进入mainActivity
            //im登陆则异步调用，无论im是否登陆成功都让用户进入主页面；
            //                    redirectTo();
            if (!"1".equals(loginBean.getInfo_status())) {//
                startActivity(new Intent(SplashActivity.this,SelecteLoginActivity.class));
            }else {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                BroadcastManager.getInstance(this).sendBroadcast(Const.DROP_LINE);//登陆Im；
            }

        } else {
            Intent intent = new Intent(mRootView.getActivity(), SelecteLoginActivity.class);
            startActivity(intent);
        }
        finish();*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (subscribe != null) {
            if (!subscribe.isDisposed()) {
                subscribe.dispose();
            }
            subscribe = null;
        }
    }
}
